package com.xx.happychatboot.middleware.rocketmq.strategy.rocketmq;

import com.xx.happychatboot.middleware.netty.domain.enums.RocketMqTypeEnum;
import com.xx.happychatboot.middleware.netty.domain.enums.WebSocketCallBackEnums;
import com.xx.happychatboot.middleware.netty.domain.vo.response.WebSocketResponse;
import com.xx.happychatboot.middleware.netty.service.WebSocketService;
import com.xx.happychatboot.middleware.rocketmq.domain.dto.RocketMqCommonDto;
import com.xx.happychatboot.serve.user.domain.entity.UserApply;
import com.xx.happychatboot.serve.user.domain.vo.response.AgreeAddFriendResponse;
import com.xx.happychatboot.serve.user.domain.vo.response.NewFriendVo;
import com.xx.happychatboot.serve.user.domain.vo.response.UserFriendVo;
import com.xx.happychatboot.serve.user.service.IUserApplyService;
import com.xx.happychatboot.serve.user.service.IUserFriendService;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Component
@Slf4j
public class AgreeAddFriendMqStrategy extends AbstractRocketMqStrategy {

    @Autowired
    private IUserApplyService userApplyService;

    @Autowired
    private IUserFriendService userFriendService;

    @Autowired
    private WebSocketService webSocketService;

    @Override
    protected RocketMqTypeEnum getStrategyCode() {
        return RocketMqTypeEnum.AGREE_ADD_FRIEND;
    }

    @Override
    public void execute(Channel channel, RocketMqCommonDto data) {
        if (Objects.isNull(channel)
                || Objects.isNull(data)
                || Objects.isNull(data.getUid())) {
            return;
        }

        log.info("同意好友消息消费用户id为{}", data.getUid());

        // 获取最新的好友申请信息
        List<NewFriendVo> list = userApplyService.getNewFriendList(data.getUid());

        // 获取最新的好友信息
        List<UserFriendVo> contactList = userFriendService.getContactList(data.getUid());

        AgreeAddFriendResponse agreeAddFriendResponse = new AgreeAddFriendResponse();
        agreeAddFriendResponse.setUid(data.getUid());
        agreeAddFriendResponse.setContactList(contactList);
        agreeAddFriendResponse.setUserApplyList(list);

        WebSocketResponse webSocketResponse = new WebSocketResponse<>();
        webSocketResponse.setData(agreeAddFriendResponse);
        webSocketResponse.setType(WebSocketCallBackEnums.AGREE_ADD_FRIEND.getType());
        webSocketService.sendMsg(channel, webSocketResponse);
    }
}
